var CustomerInfoDlg = {CustomerInfoData: {}, zTreeInstance: null, validateFields: {}};
//清除数据
CustomerInfoDlg.clearData = function () {
    this.customerInfoData = {};
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
CustomerInfoDlg.set = function (key, val) {
    this.customerInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
CustomerInfoDlg.get = function (key) {
    return $("#" + key).val();
};
//关闭此对话框
CustomerInfoDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};
//收集数据
CustomerInfoDlg.collectData = function () {
    this.set('id').set('customerName').set('pseudonym').set('customerPhone').set('customerSex').set('customerAge')
        .set('birthday').set('customerStatus').set('xcustomerStatus').set('referee').set('refereeShip').set('idCardNo').set('fixedTelephone')
        .set('qqNumber').set('wechatNumber').set('isHaveChildren').set('isReturnVisit').set('email').set('zipCode').set('identificationNumber')
        .set('numberOfAudits').set('finalAuditor').set('auditorTime').set('consumAbility').set('nonTransactionLevel').set('appointmentPayService')
        .set('transactionLevel').set('characters').set('consumptionHabits').set('otherContact').set('country').set('address')
        .set('province').set('cities').set('region').set('maritalStatus').set('occupation').set('filingTime').set('medicalRecordNumber')
        .set('arrivalTime').set('fieldTc').set('causeOfNoReturnVisit').set('entryClerk').set('recordPeople').set('reservationsId')
        .set('oneChannel').set('twoChannel').set('thrChannel').set('oneMedium').set('twoMedium').set('thrMedium').set('department').set('treatmentUnit')
        .set('archiving').set('toVoidremarks').set('remarks').set('actualAccountBalance').set('giveAccountBalance').set('refundMoney')
        .set('scarchiving').set('treatmentUnit').set('phoneSharShip');
};
//验证数据是否为空
CustomerInfoDlg.validate = function () {
    $('#customerInfoForm').data("bootstrapValidator").resetForm();
    $('#customerInfoForm').bootstrapValidator('validate');
    return $("#customerInfoForm").data('bootstrapValidator').isValid();
};
//添加推荐人
CustomerInfoDlg.getReferee = function () {
    var index = layer.open({
        type: 2,
        title: '添加推荐人',
        area: ['50%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customer/to_referee'
    });
    this.layerIndex = index;
};
//修改患者信息
CustomerInfoDlg.updateCustomer = function () {
    this.clearData();
    this.collectData();
    var customerId = $('#id').val();
    var customerName = $("#customerName").val();   //姓名
    var province = $("#province").val();   //省份
    var cities = $("#cities").val();   //市
    var customerAge = $("#customerAge").val();   //年龄
    var birthday = $("#birthday").val();   //生日
    var medicalRecordNumber = $("#medicalRecordNumber").val();   //标识号
    var customerSex = $("#customerSex").val();   //性别
    var region = $("#region").val();  //区域customerStatus
    var customerStatus = $("#customerStatus").val();  //客户状态
    if (customerName == null || customerName == "") {
        alert("请输入姓名！");
        return;
    }
    if (province == null || province == "") {
        alert("请输入省份！");
        return;
    }
    if (cities == null || cities == "") {
        alert("请输入城市！");
        return;
    }
    if (customerAge == null || customerAge == "") {
        alert("请输入年龄！");
        return;
    }
    if (birthday == null || birthday == "") {
        alert("请输入生日！");
        return;
    }
    if (medicalRecordNumber == null || medicalRecordNumber == "") {
        alert("请输入病历号！");
        return;
    }
    if (customerSex == null || customerSex == "") {
        alert("请输入性别！");
        return;
    }
    if (region == null || region == "") {
        alert("请输入区域！");
        return;
    }
    if (customerStatus == null || customerStatus == "") {
        alert("请输入客户状态！");
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/customer/update_CustomeInfoValue/" + customerId, function (data) {
        if (data == "0") {
            alert("病历号已存在！");
            window.parent.Customer.table.refresh();
            CustomerInfoDlg.close();
        } else {
            Feng.success("修改成功!");
            window.parent.Customer.table.refresh();
            CustomerInfoDlg.close();
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    });
    ajax.set(this.customerInfoData);
    ajax.start();
};

$(function () {
    $.post(Feng.ctxPath + "/character/getList", function (data) {
        var txt = '<option value="">--请选择性格--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].characters + '">' + data[i].characters + '</option>';
        }
        document.getElementById('characters').innerHTML = txt;
        var seletecharacters = $('#charactersValue').val();
        if (seletecharacters != "") {
            $("#characters option[value='" + seletecharacters + "']").attr("selected", "selected");
        }
    }, "json");
    $.post(Feng.ctxPath + "/refereeShip/getList", function (data) {
        var txt = '<option value="">--请选择与推荐人关系--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].refereeShip + '">' + data[i].refereeShip + '</option>';
        }
        document.getElementById('refereeShip').innerHTML = txt;
        var seleterefereeShip = $('#refereeShipValue').val();
        if (seleterefereeShip != "") {
            $("#refereeShip option[value='" + seleterefereeShip + "']").attr("selected", "selected");
        }
    }, "json");
        var seleteoccupation = $('#occupationValue').val();
        if (seleteoccupation != "") {
            $("#occupation option[value='" + seleteoccupation + "']").attr("selected", "selected");
        }
    $.post(Feng.ctxPath + "/consumptionHabits/getList", function (data) {
        var txt = '<option value="">--请选择消费习惯--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].consumption_habits + '">' + data[i].consumption_habits + '</option>';
        }
        document.getElementById('consumptionHabits').innerHTML = txt;
        var seleteconsumptionHabits = $('#consumptionHabitsValue').val();
        if (seleteconsumptionHabits != "") {
            $("#consumptionHabits option[value='" + seleteconsumptionHabits + "']").attr("selected", "selected");
        }
    }, "json");
    $.post(Feng.ctxPath + "/consumAbility/getList", function (data) {
        var txt = '<option value="">--请选择消费能力--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].consum_ability + '">' + data[i].consum_ability + '</option>';
        }
        document.getElementById('consumAbility').innerHTML = txt;
        var seleteconsumAbility = $('#consumAbilityValue').val();
        if (seleteconsumAbility != "") {
            $("#consumAbility option[value='" + seleteconsumAbility + "']").attr("selected", "selected");
        }
    }, "json");
    if ($("#customerSexValue").val() == "男") {
        $("#customerSex option[value='1']").attr("selected", "selected");
    } else if ($("#customerSexValue").val() == "女") {
        $("#customerSex option[value='2']").attr("selected", "selected");
    }
    $("#birthday").on("input", function () {
        var resbirthday = /^(19\d\d|20\d\d)[-/ ](0\d|1[0-2])[-/ ](0\d|[1-2]\d|30|31)$/;
        var birthday = $("#birthday").val();
        if (birthday.length > 0 && birthday.length < 4) {
        } else if (birthday.length >= 4 && birthday.length < 10) {
            var s = birthday.substring(0, 4);
            if (!isNaN(s)) {
                var data = new Date();
                $("#customerAge").val(data.getFullYear() - s)
            }
        } else {
            if (resbirthday.test(birthday)) {
                var birthDayTime = new Date(birthday).getTime();
                var nowTime = new Date().getTime();
                $("#customerAge").val(Math.ceil((nowTime - birthDayTime) / 31536000000))
            } else {
                Feng.error("请输入正确的格式！")
            }
        }
    });
    $("#customerAge").on('input', function () {
        var date = new Date();
        var currentYear = date.getFullYear() - $("#customerAge").val();
        $("#birthday").val(currentYear + "-01-01")
    });
});